package com.jiaai.cloud.wx.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jiaai.cloud.base.entity.RestResultGenerator;
import com.jiaai.cloud.base.exception.ServiceException;
import com.jiaai.cloud.base.page.CustomPage;
import com.jiaai.cloud.base.page.FrontPage;
import com.jiaai.cloud.wx.entity.TLifecycle;
import com.jiaai.cloud.wx.service.ITLifecycleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 生活圈信息表 前端控制器
 * </p>
 *
 * @author 佳爱网络科技
 * @since 2017-09-26
 */
@Controller
@RequestMapping("/api/v1/tlifecycle")
public class TLifecycleController {

    @Autowired
    private ITLifecycleService itLifecycleService;

    /**
     * 新添加一条, 生活圈文章
     * @param tLifecycle 里面需要shopid
     * @return
     * @throws ServiceException
     */
    @RequestMapping(value = "/alifecycle",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"} )
    @ResponseBody
    public Object addALifecycle(@RequestBody TLifecycle tLifecycle) throws ServiceException {
        try {
            itLifecycleService.insert(tLifecycle);
            return RestResultGenerator.genSuccessResult("添加成功");
        } catch (Exception e){
            throw new ServiceException("添加失败", e.getMessage());
        }
    }

    /**
     * 查询生活圈文章列表, 分页
     * @param frontPage
     * @return
     * @throws ServiceException
     */
    @RequestMapping(value = "/lifecycles",method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"} )
    @ResponseBody
    public Object queryForAll(FrontPage frontPage) throws ServiceException {
        Page page = frontPage.getPagePlus();
        page.setOpenSort(false);page.setSize(5);page.setSearchCount(false);
        EntityWrapper<TLifecycle> ew = new EntityWrapper<TLifecycle>();
        ew.setSqlSelect("shopid, lc_id as lcId, lc_titile as lcTitile, lc_abstract as lcAbstract, lc_pics as lcPics");
        try {
            return RestResultGenerator.genSuccessResult(new CustomPage<>(itLifecycleService.selectPage(page, ew)));
        } catch (Exception e){
            throw new ServiceException("查询失败", e.getMessage());
        }
    }

    /**
     * 查询文章的详细内容
     * @param id
     * @return
     * @throws ServiceException
     */
    @RequestMapping(value = "/alifecycle/{id}",method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"} )
    @ResponseBody
    public Object queryForAll(@PathVariable("id") Integer id) throws ServiceException {
        Wrapper<TLifecycle> ew = new EntityWrapper<>();
        ew.setSqlSelect("lc_content as lcContent")
                .eq("lc_id", id);
        try {
            return RestResultGenerator.genSuccessResult(itLifecycleService.selectOne(ew));
        } catch (Exception e){
            throw new ServiceException("查询失败", e.getMessage());
        }
    }

    /**
     * 查询本人的生活圈列表, 以便编辑, 分页
     * @param userid
     * @return
     * @throws ServiceException
     */
    @RequestMapping(value = "/mylifecycles",method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"} )
    @ResponseBody
    public Object queryForMyLifeCycleList(@RequestHeader Integer userid, FrontPage frontPage) throws ServiceException {
        try {
            return RestResultGenerator.genSuccessResult(itLifecycleService.queryForMyLifeCycleList(userid, frontPage.getPagePlus()));
        } catch (Exception e){
            throw new ServiceException("查询失败", e.getMessage());
        }
    }

    /**
     * 查询本人的一条生活圈, 以便编辑
     * @param id
     * @return
     * @throws ServiceException
     */
    @RequestMapping(value = "/mylifecycle/{id}",method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"} )
    @ResponseBody
    public Object queryForMyLifeCycle(@PathVariable("id") Integer id) throws ServiceException {
        try {
            return RestResultGenerator.genSuccessResult(itLifecycleService.selectById(id));
        } catch (Exception e){
            throw new ServiceException("查询失败", e.getMessage());
        }
    }

    /**
     * 编辑本人的生活圈
     * @param tLifecycle 里面必须包含lcId
     * @return
     * @throws ServiceException
     */
    @RequestMapping(value = "/mylifecycle",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"} )
    @ResponseBody
    public Object updateMyLifeCycle(@RequestBody TLifecycle tLifecycle) throws ServiceException {
        try {
            itLifecycleService.updateById(tLifecycle);
            return RestResultGenerator.genSuccessResult("编辑成功");
        } catch (Exception e){
            throw new ServiceException("编辑失败", e.getMessage());
        }
    }

    /**
     * 删除id对应的生活圈
     * @param id
     * @return
     * @throws ServiceException
     */
    @RequestMapping(value = "/mylifecycle/{id}",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"} )
    @ResponseBody
    public Object deleteMyLifeCycle(@PathVariable("id") Integer id) throws ServiceException {
        try {
            itLifecycleService.deleteById(id);
            return RestResultGenerator.genSuccessResult("删除成功");
        } catch (Exception e){
            throw new ServiceException("删除失败", e.getMessage());
        }
    }
}
